
import {getLyric} from "api/song";
import {ERR_OK} from "api/config";
import {Base64} from 'js-base64';
import {commonParams} from "../../api/config"; // 引入js-base64对歌词进行解码

import {getMusic} from "api/singer"; // 获取歌曲vKey;

import axios from 'axios';
export default class Song {
  constructor({id, mid, singer, name, album, duration, image, url}) {
    this.id = id;
    this.mid = mid;
    this.singer = singer;
    this.name = name;
    this.album = album;
    this.duration = duration;
    this.image = image;
    this.url = url;
  }
  // 个Song扩展一个 歌词的方法属性
  getLyric () {
    if (this.lyric) {
      return Promise.resolve(this.lyric)
    }
    return new Promise((resolve,reject) => {
      getLyric(this.mid).then((res) => {
        if (res.retcode === ERR_OK) {
          this.lyric = Base64.decode(res.lyric);// 使用js-base64进行解码歌词
          resolve(this.lyric);
        } else {
          reject('no lyric')
        }
      })
    });

  }
}

// export function createSong(musicData) {
//  // http://120.41.44.18/amobile.music.tc.qq.com/C400000Ky4BK3cdOQ6.m4a?guid=5747429780&vkey=15D63880D85B6FE3D87FDB8D069E460E7B79C46DBE54E54D4427DB09CED8D54F500C99C4BDD2553E3FED074E9A71394B235582C53081F566&uin=0&fromtag=66
//   return new Song({
//     id: musicData.songid,
//     mid: musicData.songmid,
//     singer: filterSinger(musicData.singer),
//     name: musicData.songname,
//     album: musicData.albumname,
//     duration: musicData.interval,
//     image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
//     // 以下 的QQ音乐播放地址有可能会变化。自行百度，需要打开QQ音乐去查看抓取拼接数据
//     url: `http://ws.stream.qqmusic.qq.com/C100${musicData.songmid}.m4a?fromtag=0&guid=126548448`
//   })
// }

// export function createSong (musicData, songVkey, guid) {
//   return new Song({
//     id: musicData.songid,
//     mid: musicData.songmid,
//     singer: filterSinger(musicData.singer),
//     name: musicData.songname,
//     album: musicData.albumname,
//     duration: musicData.interval,
//     image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
//
//     url: `http://dl.stream.qqmusic.qq.com/C400${musicData.songmid}.m4a?vkey=${songVkey}&guid=5747429780&uin=0&fromtag=66`
//   })
// }


// 获取歌曲的 拼接成歌曲的url
export function createSong(musicData,songVkey,guid) {
  return new Song({
    id: musicData.songid,
    mid: musicData.songmid,
    singer: filterSinger(musicData.singer),
    name: musicData.songname,
    album: musicData.albumname,
    duration: musicData.interval,
    image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
    url: `http://dl.stream.qqmusic.qq.com/C400${musicData.songmid}.m4a?vkey=${songVkey}&guid=5747429780&uin=0&fromtag=66`
  })
}

// 处理一首歌曲由两个歌手的情况
export function filterSinger(singer) {
  let ret = [];
  if (!singer) {
    return ''
  }
  singer.forEach((s) => {
    ret.push(s.name);
  });
  return ret.join('/')
}
